using System.Text;
using ChessKit.Common;

namespace EmraldClient
{
  public static class Utils
  {
    public static BoardLocation ToLocation(string value)
    {
      var f = 63-int.Parse(value);
      return BoardLocation.Cell(8 - f % 8, f / 8 + 1);
    }
    public static Position ToPosition(string value)
    {
      return Pos(ToLocation(value));
    }

    public static string ToFen(string value)
    {
      var str = new StringBuilder(value);
      var i = 0;
      var global = 0;
      while (i < str.Length)
      {
        var counter = 0;
        while (str[i + counter] == 'x')
        {
          counter++;
          if (++global % 8 == 0)
            break;
        }
        if (counter > 0)
        {
          str.Remove(i, counter);
          str.Insert(i, counter);
        }
        else global++;
        i++;
        
        if (global == 64) break;
        if (global % 8 == 0)
          str.Insert(i++, '/');
      }
      str.Append(" KQkq - 0 1");
      return str.ToString();
    }

    public static Position Pos(BoardLocation boardLocation)
    {
      return new Position(boardLocation.X - 1, boardLocation.Y - 1);
    }

    public static BoardLocation Pos(Position pos)
    {
      return BoardLocation.Cell(pos.X + 1, pos.Y + 1);
    }

  }
}